package com.soccertgc.core.game
{
	import com.soccertgc.core.cards.Deck;

	public class GameDeck
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------

		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _actionCards:Vector.<GameActionCard>;
		private var _playerCards:Vector.<GamePlayerCard>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameDeck(deck:Deck = null)
		{
			// Init objects
			_actionCards = new Vector.<GameActionCard>();
			_playerCards = new Vector.<GamePlayerCard>();
			//
			if (deck != null) set(deck);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get actionCards():Vector.<GameActionCard>
		{
			return _actionCards;
		}
		
		public function get playerCards():Vector.<GamePlayerCard>
		{
			return _playerCards;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		protected function getActionCardIndex(instanceId:uint):int
		{
			for (var i:int = 0; i<_actionCards.length; i++)
			{
				if (_actionCards[i].instanceId == instanceId) return i;
			}
			return -1;
		}
		
		protected function getPlayerCardIndex(instanceId:uint):int
		{
			for (var i:int = 0; i<_playerCards.length; i++)
			{
				if (_playerCards[i].instanceId == instanceId) return i;
			}
			return -1;
		}
		
		public function equalsActionCards(gameDeck:GameDeck):Boolean
		{
			// Check cards number
			if (_actionCards.length != gameDeck.actionCards.length) return false;
			// Check each card
			for (var i:int = 0; i < _actionCards.length; i++) if (!_actionCards[i].equals(gameDeck.actionCards[i])) return false;
			// Equal
			return true;
		}
		
		public function equalsPlayerCards(gameDeck:GameDeck):Boolean
		{
			// Check cards number
			if (_playerCards.length != gameDeck.playerCards.length) return false;
			// Check each card
			for (var i:int = 0; i < _playerCards.length; i++) if (!_playerCards[i].equals(gameDeck.playerCards[i])) return false;
			// Equal
			return true;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
				
		public function containsActionCard(instanceId:uint):Boolean
		{
			return getActionCardIndex(instanceId) > -1;
		}
		
		public function containsPlayerCard(instanceId:uint):Boolean
		{
			return getPlayerCardIndex(instanceId) > -1;
		}
		
		public function findActionCard(instanceId:uint):GameActionCard
		{
			var card:GameActionCard;
			var index:int = getActionCardIndex(instanceId);
			if (index > -1) card = _actionCards[index];
			return card;
		}
		
		public function findPlayerCard(instanceId:uint):GamePlayerCard
		{
			var card:GamePlayerCard;
			var index:int = getPlayerCardIndex(instanceId);
			if (index > -1) card = _playerCards[index];
			return card;
		}		
		
		public function set(deck:Deck):void
		{
			// Temporary lists
			var actions:Vector.<GameActionCard> = new Vector.<GameActionCard>();
			var players:Vector.<GamePlayerCard> = new Vector.<GamePlayerCard>();
			// Loop over deck action cards
			for (var i:int = 0; i < deck.actionCardsCount; i++)
			{
				// Create a new GameActionCard and copy data from the original one
				var gameActionCard:GameActionCard = new GameActionCard();
				gameActionCard.copy(deck.getActionCard(i));
				// Add the card to the list
				actions.push(gameActionCard);
			}
			// Clone all players from deck
			players.push(new GamePlayerCard(deck.goalkeeper));
			players.push(new GamePlayerCard(deck.defender01));
			players.push(new GamePlayerCard(deck.defender02));
			players.push(new GamePlayerCard(deck.midfield));
			players.push(new GamePlayerCard(deck.forward01));
			players.push(new GamePlayerCard(deck.forward02));
			players.push(new GamePlayerCard(deck.substitutionDefence));
			players.push(new GamePlayerCard(deck.substitutionMidfield));
			players.push(new GamePlayerCard(deck.substitutionForward));
			// Clear current cards
			clear();
			// Popolate cards lists
			for (var x:int = 0; x < actions.length; x++) _actionCards.push(actions[x]);
			for (var y:int = 0; y < players.length; y++) _playerCards.push(players[y]);
		}
		
		public function equals(gameDeck:GameDeck):Boolean
		{
			// Equal
			return	equalsActionCards(gameDeck) &&
					equalsPlayerCards(gameDeck);
		}
		
		public function copy(gameDeck:GameDeck):void
		{
			// Temporary lists
			var actions:Vector.<GameActionCard> = new Vector.<GameActionCard>();
			var players:Vector.<GamePlayerCard> = new Vector.<GamePlayerCard>();
			// Loop over deck action cards
			for (var i:int = 0; i < gameDeck.actionCards.length; i++)
			{
				// Add the card to the list
				actions.push(gameDeck.actionCards[i].clone());
			}
			// Loop over deck action cards
			for (var j:int = 0; j < gameDeck.playerCards.length; j++)
			{
				// Add the card to the list
				players.push(gameDeck.playerCards[j].clone());
			}
			// Clear current cards			
			clear();
			// Popolate cards lists
			for (var x:int = 0; x < actions.length; x++) _actionCards.push(actions[x]);
			for (var y:int = 0; y < players.length; y++) _playerCards.push(players[y]);
		}
		
		public function clone():GameDeck
		{
			var gameDeck:GameDeck = new GameDeck();
			gameDeck.copy(this);
			return gameDeck;
		}
		
		public function clear():void
		{
			_actionCards.splice(0, _actionCards.length);
			_playerCards.splice(0, _playerCards.length);
		}
	}
}